package com.mbyte.easy.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mbyte.easy.admin.entity.*;
import com.mbyte.easy.admin.mapper.DraftLogMapper;
import com.mbyte.easy.admin.service.*;
import com.mbyte.easy.security.entity.SysUser;
import com.mbyte.easy.security.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.mbyte.easy.common.constant.DraftLogConstant.ACTION_MAP;

/**
 * <p>
 * 操作记录表 服务实现类
 * </p>
 *
 * @author lp
 * @since 2020-01-08
 */
@Slf4j
@Service
public class DraftLogServiceImpl extends ServiceImpl<DraftLogMapper, DraftLog> implements IDraftLogService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IDraftService draftService;
    @Autowired
    private IPptService pptService;
    @Autowired
    private IAudioService audioService;
    @Autowired
    private IVideoService videoService;
    @Autowired
    private IGroupService groupService;

    /**
     * @Description: 根据稿件id获取稿件日志信息
     * @param draftId
     * @return: java.util.List<com.mbyte.easy.admin.entity.DraftLog> 
     * @Author: lxt 
     * @Date: 2020/3/5 17:58
     * 序号  操作类型  操作详情   组名称  操作前状态  操作后状态   操作人员  操作时间
     */
    @Override
    public List<DraftLog> listByDraftId(long draftId){
        // 缓存用户名信息
        Map<Long,String> userMap = new HashMap<>();
        List<DraftLog> resultList = new ArrayList<>();
        List<String> groupIdList = new ArrayList<>();

        // 根据稿件id直接获取日志信息
        List<DraftLog> draftLogList = list(new QueryWrapper<DraftLog>().lambda().eq(DraftLog::getDraftId,draftId));
        if(!CollectionUtils.isEmpty(draftLogList)){
            resultList.addAll(draftLogList);
            log.info("========里面是44个嘛？========"+resultList.size());
        }

        // 根据稿件id所属组id获取文字稿件日志信息
        Draft draft = draftService.getById(draftId);
        if(draft != null){
            // 已按组分配稿件
            if(StringUtils.isNoneBlank(draft.getGroupId())){
                groupIdList.add(draft.getGroupId());
            }
        }
        // 根据稿件id 获取对应ppt稿件
        Ppt ppt = pptService.getOne(new QueryWrapper<Ppt>().lambda().eq(Ppt::getDraftId,draftId));
        if(ppt != null){
            // 已按组分配稿件
            if(StringUtils.isNoneBlank(ppt.getGroupId())){
                groupIdList.add(ppt.getGroupId());
            }
        }
        // 根据稿件id 获取音频稿件
        Audio audio = audioService.getOne(new QueryWrapper<Audio>().lambda().eq(Audio::getDraftId,draftId));
        if(audio != null){
            // 已按组分配稿件
            if(StringUtils.isNoneBlank(audio.getGroupId())){
                groupIdList.add(audio.getGroupId());
            }
        }
        // 根据稿件id 获取视频稿件
        Video video = videoService.getOne(new QueryWrapper<Video>().lambda().eq(Video::getDraftId,draftId));
        if(video != null){
            // 已按组分配稿件
            if(StringUtils.isNoneBlank(video.getGroupId())){
                groupIdList.add(video.getGroupId());
            }
        }
        // 根据组id查询
//        if(!CollectionUtils.isEmpty(groupIdList)){
//            List<DraftLog> draftLogGroupList = list(new QueryWrapper<DraftLog>().lambda().in(DraftLog::getGroupId,groupIdList));
//            if(!CollectionUtils.isEmpty(draftLogGroupList)){
//                resultList.addAll(draftLogGroupList);
//                log.info("============里面是44个嘛？+++"+resultList.size());
//            }
//        }
        // 处理返回结果
        for (DraftLog draftLog : resultList) {
            // 用户名处理
            if(userMap.get(draftLog.getUserId())==null){
                SysUser sysUser = userService.selectByPrimaryKey(draftLog.getUserId());
                if(sysUser != null){
                    draftLog.setUserName(sysUser.getUsername());
                    userMap.put(draftLog.getUserId(),sysUser.getUsername());
                }
            }else{
                draftLog.setUserName(userMap.get(draftLog.getUserId()));
            }
            // 组名处理
            if(draftLog.getGroupId() != null){
                String groupName = groupService.getNameByGroupId(draftLog.getGroupId());
                if(StringUtils.isNoneBlank(groupName)){
                    draftLog.setGroupName(groupName);
                }
            }
            if(StringUtils.isNoneBlank(draftLog.getActionType())){
                draftLog.setActionType(ACTION_MAP.get(draftLog.getActionType()));
            }
        }
        // 对返回结果进行正序排序
        draftLogList.sort(Comparator.comparing(DraftLog::getCreateTime));
        return resultList;
    }
}
